package logic

import (
	"bw-renting/common"
	"bw-renting/model/mysql"
	"context"
	"errors"

	"bw-renting/order-rpc/internal/svc"
	"bw-renting/order-rpc/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderPayLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderPayLogic {
	return &OrderPayLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// todo：订单支付
func (l *OrderPayLogic) OrderPay(in *order.OrderPayReq) (*order.OrderPayRes, error) {
	// todo: add your logic here and delete this line
	orderId := in.OrderId
	houseOrder := mysql.GetOrderInfo(orderId)
	if houseOrder.ID == 0 {
		return nil, errors.New("订单信息不存在")
	}
	alipay := new(common.Alipay)
	url, err := alipay.Payment(houseOrder)
	if err != nil {
		return nil, errors.New("调用支付接口失败")
	}
	return &order.OrderPayRes{
		PayUrl: url,
	}, nil
}
